package org.jeecg.modules.ws.test;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.ws.config.BankConfig;
import org.jeecg.modules.ws.config.WyzlConfig;
import org.jeecg.modules.ws.job.BankBalanceJob;
import org.jeecg.modules.ws.job.BankDetailJob;
import org.jeecg.modules.ws.util.WyzlClient;
import org.jeecg.modules.ws.util.WyzlAccountDetailRequest;
import org.jeecg.modules.ws.util.WyzlBalanceRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 网银直连测试类
 */
@Slf4j
@RestController
@RequestMapping("/wsTest")
public class WyzlTestController {

    @Autowired
    private WyzlClient wyzlClient;

    @Autowired
    private BankConfig bankConfig;

    @Autowired
    private WyzlConfig wyzlConfig;

    @Autowired
    private BankDetailJob bankDetailJob;

    @Autowired
    private BankBalanceJob bankBalanceJob;

    /**
     * 测试余额查询
     */
    @GetMapping("/testBalance")
    public void testBalance() {
        try {
            // 获取第一个账户配置
            BankConfig.AccountConfig account = bankConfig.getConfigs().get(0);

            // 构建余额查询请求
            WyzlBalanceRequest request = new WyzlBalanceRequest();
            request.setCustId(account.getCustId());
            request.setUserId(account.getOperatorCode());
            request.setPassword(wyzlConfig.getPassword());
            request.setLanguage("CN");

            // 设置交易信息
            WyzlBalanceRequest.TxInfo txInfo = new WyzlBalanceRequest.TxInfo();
            txInfo.setAccNo(account.getAccNo());
            txInfo.setCurrCod("156"); // 人民币
            request.setTxInfo(txInfo);

            // 发送请求
            Object response = wyzlClient.queryBalance(request);
            log.info("余额查询结果: {}", JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("余额查询异常", e);
        }
    }

    /**
     * 测试账户明细查询
     */
    @GetMapping("/testAccountDetail")
    public void testAccountDetail() {
        try {
            // 获取第一个账户配置
            BankConfig.AccountConfig account = bankConfig.getConfigs().get(0);

            // 构建账户明细查询请求
            WyzlAccountDetailRequest request = new WyzlAccountDetailRequest();
            request.setCustId(account.getCustId());
            request.setUserId(account.getOperatorCode());
            request.setPassword(wyzlConfig.getPassword());
            request.setLanguage("CN");

            // 设置查询时间范围（最近30天）
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Date now = new Date();
            String endDate = sdf.format(now);
            Date startDate = new Date(now.getTime() - 30L * 24 * 60 * 60 * 1000);
            String startDateStr = sdf.format(startDate);

            // 设置交易信息
            WyzlAccountDetailRequest.TxInfo txInfo = new WyzlAccountDetailRequest.TxInfo();
            txInfo.setAccNo1(account.getAccNo());
            txInfo.setCurrCod("156"); // 人民币
            txInfo.setStartDate(startDateStr);
            txInfo.setEndDate(endDate);
            txInfo.setPage("1"); // 第一页
            txInfo.setTotalRecord("10"); // 每页10条
            request.setTxInfo(txInfo);

            // 发送请求
            Object response = wyzlClient.queryAccountDetail(request);
            log.info("账户明细查询结果: {}", response);
        } catch (Exception e) {
            log.error("账户明细查询异常", e);
        }
    }

    /**
     * 测试账户明细查询定时任务
     */
    @GetMapping("/testQueryAccountDetail")
    public Map<String, Object> testQueryAccountDetail() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("开始测试账户明细查询定时任务");
            long startTime = System.currentTimeMillis();

            // 执行查询
            bankDetailJob.queryAccountDetail();

            long endTime = System.currentTimeMillis();
            result.put("success", true);
            result.put("message", "账户明细查询测试完成");
            result.put("executionTime", (endTime - startTime) / 1000.0 + "秒");
            log.info("账户明细查询测试完成，耗时：{}秒", (endTime - startTime) / 1000.0);
        } catch (Exception e) {
            log.error("账户明细查询测试异常", e);
            result.put("success", false);
            result.put("message", "账户明细查询测试异常：" + e.getMessage());
        }
        return result;
    }

    /**
     * 测试账户余额
     */
    @GetMapping("/testQueryBalance")
    public Map<String, Object> testQueryBalance() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("开始测试账户明细查询定时任务");
            long startTime = System.currentTimeMillis();

            // 执行查询
            bankBalanceJob.queryBalance();

            long endTime = System.currentTimeMillis();
            result.put("success", true);
            result.put("message", "账户明细查询测试完成");
            result.put("executionTime", (endTime - startTime) / 1000.0 + "秒");
            log.info("账户明细查询测试完成，耗时：{}秒", (endTime - startTime) / 1000.0);
        } catch (Exception e) {
            log.error("账户明细查询测试异常", e);
            result.put("success", false);
            result.put("message", "账户明细查询测试异常：" + e.getMessage());
        }
        return result;
    }
}
